﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _ISYC_309_01
    {
        public static void ISYC_309_01(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 3; //放大系数


            double b = Convert.ToDouble(data["b"]) * scale;
            double l3 = Convert.ToDouble(data["l"]) * scale;
            double wf = Convert.ToDouble(data["wf"]) * scale;
            double lf = Convert.ToDouble(data["lf"]) * scale;
            double lh = Convert.ToDouble(data["lh"]) * scale;
            double kapr = Convert.ToDouble(data["kapr"]);
            double lfa = l3 * Math.Cos(kapr * pi / 180);
            double oal = lf + lfa;

            //坐标
            Vector2 v59 = new Vector2(0, 0);
            Vector2 v1 = new Vector2(v59.X - 2, v59.Y - b / 2);
            Vector2 v2 = new Vector2(v1.X + oal + 4, v1.Y);
            Vector2 v61 = new Vector2(v59.X + oal - b / 4.191, v59.Y - b / 4.191);
            Vector2 v60 = new Vector2(v61.X - oal / 24.856, v59.Y);
            Vector2 v62 = new Vector2(v59.X + lf, v60.Y - wf);
            Vector2 v63 = fun.Polar(v62, kapr * pi / 180, l3);
            Vector2 v64 = fun.Polar(v63, (90 + kapr) * pi / 180, l3);
            Vector2 v65 = fun.Polar(v62, (90 + kapr) * pi / 180, l3);
            Vector2 v66 = fun.Polar(v65, kapr * pi / 180, l3 / 11.371);
            Vector2 v67 = new Vector2(v63.X - lh, v59.Y - b);
            Vector2 v68 = new Vector2(v59.X, v67.Y);
            Vector2 v69 = new Vector2(v61.X, v63.Y + (v63.X - v61.X) * Math.Tan((90 - kapr) * pi / 180));
            Vector2 v74 = fun.Polar(v62, (kapr + 45) * pi / 180, l3 / Math.Sin(45 * pi / 180) / 2);
            Vector2 v75 = new Vector2(v61.X, v60.Y - b / 2);

            Vector2 a59 = fun.mirrorpoint(v59, v1, v2);
            Vector2 a60 = fun.mirrorpoint(v60, v1, v2);
            Vector2 a61 = fun.mirrorpoint(v61, v1, v2);
            Vector2 a62 = fun.mirrorpoint(v62, v1, v2);
            Vector2 a63 = fun.mirrorpoint(v63, v1, v2);
            Vector2 a64 = fun.mirrorpoint(v64, v1, v2);
            Vector2 a65 = fun.mirrorpoint(v65, v1, v2);
            Vector2 a66 = fun.mirrorpoint(v66, v1, v2);
            Vector2 a67 = fun.mirrorpoint(v67, v1, v2);
            Vector2 a68 = fun.mirrorpoint(v68, v1, v2);
            Vector2 a69 = fun.mirrorpoint(v69, v1, v2);
            Vector2 a70 = new Vector2(a67.X, a60.Y);
            Vector2 a74 = fun.mirrorpoint(v74, v1, v2);


            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(a69));
            line2.Vertexes.Add(new LwPolylineVertex(a61));
            line2.Vertexes.Add(new LwPolylineVertex(a60));
            line2.Vertexes.Add(new LwPolylineVertex(a59));
            line2.Vertexes.Add(new LwPolylineVertex(a68));
            line2.Vertexes.Add(new LwPolylineVertex(a67));
            line2.Vertexes.Add(new LwPolylineVertex(a66));
            fun.PolyFill(dxf, line2, 0.5, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(a62));
            line3.Vertexes.Add(new LwPolylineVertex(a63));
            line3.Vertexes.Add(new LwPolylineVertex(a64));
            line3.Vertexes.Add(new LwPolylineVertex(a65));
            line3.Vertexes.Add(new LwPolylineVertex(a62));
            fun.PolyFill(dxf, line3, 0.5, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(a67));
            line4.Vertexes.Add(new LwPolylineVertex(a70));
            fun.PolyFill(dxf, line4, 0.5, contour);

            fun.drawcircle(dxf, a74, l3 / 8.559, 0.5, contour);


            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;


            fun.dimrot1(dxf, a68, a62, 0, lf / 4, 1, height, arrowsize, exline, "LF", "", "");

            fun.dimrot1(dxf, a68, a63, 0, lf / 2, 1, height, arrowsize, exline, "OAL", "", "");

            fun.dimrot1(dxf, a62, v2, -90, l3 * 3, 1, height, arrowsize, exline, "WFa", "", "");

            fun.dimrot1(dxf, a62, a60, -90, l3 * 2, 1, height, arrowsize, exline, "WFb", "", "");

            fun.dimrot1(dxf, a70, a63, 180, lf / 2, 1, height, arrowsize, exline, "LH", "", "");

            fun.dimrot1(dxf, a64, a65, 180-kapr , lh / 2, 1, height, arrowsize, exline, "L", "", "");

            fun.jdbz2(dxf, new Line(a62, fun.Polar(a62, (-kapr - 90) * pi / 180, 0.1)), new Line(a62, fun.Polar(a62,  - kapr * pi / 180, 0.1)), l3 * 4, height, arrowsize, exline, "EPSR", "", "");

            fun.jdbz2(dxf, new Line(a62, fun.Polar(a62, -kapr * pi / 180, 0.1)), new Line(a62, fun.Polar(a62, 0 * pi / 180, 0.1)), l3 * 4, height, arrowsize, exline, "EPSR", "", "");


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);

            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }






            //定义格式，路径导出 
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}
